﻿using System;
using System.Collections.Generic;
using System.Linq;
using Algoritmia.Utils;

namespace Algoritmia.Problems.BinPacking
{
    public class FirstFitDecreasingBinPacker : IBinPacker
    {
        #region Implementation of IBinPacker

        public IEnumerable<int> Pack(DataStructures.Lists.IList<double> items, double capacity)
        {
            var w = items.Select((e, i) => new Tuple<double, int>(e, i)).ToList();
            w.Sort((tp1,tp2) => tp2.Item1.CompareTo(tp1.Item1));
            var list = w.Select(e => e.Item1).ToAlgoritmiaList();
            var x = new FirstFitBinPacker().Pack(list, capacity).ToList();
            var x1 = Enumerable.Repeat(0, x.Count).ToList();
            for (int i = 0; i < x.Count; i++ )
            {
                x1[w[i].Item2] = x[i];
            }
            return x1;
        }

        #endregion
    }
}
